package com.huluobo.a2307monthdemo.register

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2307monthdemo.R
import com.huluobo.a2307monthdemo.databinding.ActivityRegisterBinding
import com.huluobo.lib_base.mvvm.BaseActivity
import com.huluobo.lib_base.mvvm.MessageEvent
import com.huluobo.lib_base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        vm.registerSuccess.observe(this) {
            ToastUtils.showLong("注册成功")
            //注册成功,将用户bean回传
            EventBus.getDefault().post(MessageEvent(Gson().toJson(it)))
            finish()
        }
    }

    override fun initView() {
        binding.registerBtn.setOnClickListener {
            val username = binding.registerUsername.text.toString()
            val password = binding.registerPassword.text.toString()
            val rePassword = binding.registerRePassword.text.toString()

            if (username.isNotEmpty() && password.isNotEmpty() && rePassword.isNotEmpty()) {
                if (password == rePassword) {
                    val map = mapOf(
                        Const.PARAM_USERNAME to username,
                        Const.PARAM_PASSWORD to password,
                        Const.PARAM_RE_PASSWORD to rePassword
                    )
                    vm.register(map)

                } else {
                    binding.registerRePassword.error = "两次密码输入不一致"
                    ToastUtils.showLong("两次密码输入不一致")
                }
            } else {
                binding.registerRePassword.error = "用户名密码再次输入密码不能为空"
                ToastUtils.showLong("用户名密码再次输入密码不能为空")
            }
        }
    }
}